package com.am.mosl.cityandroid.citychange;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.am.mosl.cityandroid.util.ComparatorCity;
import com.am.mosl.cityandroid.R;
import com.am.mosl.cityandroid.bean.City;
import com.sina.weibo.sdk.utils.LogUtil;

import java.util.Collections;
import java.util.List;

/**
 * Created by mosl on 14-7-11.
 */
public class CitySortAdapter extends BaseAdapter /*implements SectionIndexer */{

    private Context context;
    private List<City> mCityCharsList;

    public CitySortAdapter(Context context, List<City> m) {
        this.context = context;
        this.mCityCharsList = m;
    }

    public void applend(List<City> m) {
        mCityCharsList.addAll(m);
        Collections.sort(mCityCharsList, new ComparatorCity());
        notifyDataSetChanged();
    }

    public void applendHotCity(List<City> hot) {
        this.mCityCharsList.addAll(0, hot);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mCityCharsList.size();
    }

    @Override
    public Object getItem(int position) {
        return mCityCharsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        City city = null;
        String catalog = "";
        String lastCatalog = "";
        ViewHolder viewHolder = null;
        city = mCityCharsList.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.sort_listview_city_item, null);
            viewHolder = new ViewHolder();
            viewHolder.tvCatalog = (TextView) convertView.findViewById(R.id.catalog);
            viewHolder.City = (TextView) convertView.findViewById(R.id.title);
            viewHolder.city_id = (TextView) convertView.findViewById(R.id.city_id);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        catalog = city.getInitial();
        if (position == 0) {
            viewHolder.tvCatalog.setVisibility(View.VISIBLE);
            viewHolder.tvCatalog.setText(catalog);
        } else {

            lastCatalog = mCityCharsList.get(position - 1).getInitial();
            if (catalog.equals(lastCatalog)) {
                viewHolder.tvCatalog.setVisibility(View.GONE);
            } else {
                viewHolder.tvCatalog.setVisibility(View.VISIBLE);
                viewHolder.tvCatalog.setText(catalog);
            }
        }
        viewHolder.City.setText(city.getCity());
        viewHolder.city_id.setText(Integer.toString(city.getId()));
        if (city.getIsopen() == 0)
            viewHolder.City.setTextColor(context.getResources().getColor(R.color.black));
        else
            viewHolder.City.setTextColor(context.getResources().getColor(R.color.red));
        return convertView;
    }

    static class ViewHolder {
        TextView tvCatalog;//目录
        TextView City;//城市
        TextView city_id;
    }

//    @Override
//    public Object[] getSections() {
//        return new Object[0];
//    }
//
//    @Override
//    public int getPositionForSection(int sectionIndex) {
//        return 0;
//    }
//
//    @Override
//    public int getSectionForPosition(int position) {
//        char Initial = (char) position;
//        if (Initial == '#') {
//            return 0;
//        }
//        for (int i = 0; i < mCityCharsList.size(); i++) {
//            String l = mCityCharsList.get(i).getInitial();
//            char firstChar = l.toUpperCase().charAt(0);
//            if (firstChar == Initial) {
//                return i;
//            }
//        }
//        return -1;
//    }
}
